package com.ming.autodt;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        needStop = false;
        LoopThread loopThread = new LoopThread();
        loopThread.start();

    }

    public void openDingTalk(View view) {
//        com.alibaba.android.rimet
        PackageManager packageManager = getPackageManager();
        Intent intent = new Intent();
        intent = packageManager.getLaunchIntentForPackage("com.alibaba.android.rimet");
        if (intent == null) {
            Toast.makeText(this, "未安装", Toast.LENGTH_LONG).show();
        } else {
            startActivity(intent);
        }
    }

    String TAG = "MainActivity";

    public void goBack() {
        //这样返回方式似乎受限很多，必须是本应用打开情况下，然后本应用打开其他应用，才能再跳转回来
        //尝试手动切换到桌面，就跳不回来了
//        startActivity(new Intent(MainActivity.this, MainActivity.class));
        ActivityManager ams = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTasks = ams.getRunningTasks(100);
        Log.i(TAG, "goBack: ");
        for (int i = 0; i < runningTasks.size(); i++) {
            ActivityManager.RunningTaskInfo taskInfo = runningTasks.get(i);
            if (taskInfo.topActivity != null) {
                Log.i(TAG, "goBack: taskinfo:" + taskInfo.topActivity.getPackageName());
                if (taskInfo.topActivity.getPackageName().equals(getPackageName())) {
                    Log.i(TAG, "goBack: moveTaskToFront");
                    ams.moveTaskToFront(taskInfo.taskId, 0);
                }
            }
        }
    }

    class LoopThread extends Thread {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        //        ArrayList min
        Random random = new Random();

        @Override
        public void run() {
            super.run();
            while (true) {
                //这里是手机系统时间
                Calendar calendar = Calendar.getInstance();
                int dayIndexInWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; //要减1因为老外周日是第一天
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int am_pm = calendar.get(Calendar.AM_PM); //0为上午，1为下午
                Log.i(TAG, "run: now is " + weekDays[dayIndexInWeek] +
                        "; am_pm:" + am_pm + "; " + hour + ":" + minute);

                int morningStartTime = random.nextInt(2) + 43; //43-44
                int nightStartTime = random.nextInt(2) + 33; //33-34
                Log.i(TAG, "run:  morningStartTime is:" + morningStartTime +
                        "; nightStartTime is" + nightStartTime);
                //跳过周末,仅周一到周五跳转
                if (dayIndexInWeek >= 1 && dayIndexInWeek <= 5) {
                    //上午打卡
                    if (am_pm == Calendar.AM &&
                            hour == 8 &&
                            (minute >= morningStartTime && minute <= 46)) {
                        openDingTalk(null);
                        Log.i(TAG, "run: 打开DingTalk");
                        //下午打卡
                    } else if (am_pm == Calendar.PM &&
                            hour == 6 &&
                            (minute >= nightStartTime && minute <= 36)) {
                        openDingTalk(null);
                        Log.i(TAG, "run: 打开DingTalk");
                    } else {
                        //回到本页面
                        goBack();
                    }
                } else {
                    //周末不做任何处理
                    Log.i(TAG, "run: now is weekend, do nothing:" + weekDays[dayIndexInWeek]);
                }


                try {
                    // 休眠/检测时间不再固定，以达到非周期性的目的
                    // 每次跳过的时间设定为50-80秒之间
                    int sleepTime = random.nextInt(30) + 50;
                    Log.i(TAG, "run: sleep Time is " + sleepTime + "s");
                    Thread.sleep(sleepTime * 1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    boolean needStop = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        needStop = true;
    }
}